<?php

class ControllerFAPurchaseOrder extends Controller {

    private $error = array();

    public function index() {
    
        $this->language->load('fa/purchase_order');

        $this->load->model('fa/supplier');
        
        $this->document->setTitle($this->language->get('heading_title'));
        
        $this->data['heading_title'] = $this->language->get('heading_title');
        
        $this->data['column_purchase_code'] = $this->language->get('column_purchase_code');
        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_quantity'] = $this->language->get('column_quantity');
        $this->data['column_unit'] = $this->language->get('column_unit');
        $this->data['column_price'] = $this->language->get('column_price');
        $this->data['column_action'] = $this->language->get('column_action');
        
        $this->data['entry_supplier'] = $this->language->get('entry_supplier');
        $this->data['entry_order_date'] = $this->language->get('entry_order_date');
        $this->data['entry_address'] = $this->language->get('entry_address');
        
        $this->data['button_add_item'] = $this->language->get('button_add_item');
        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        
        $url = '';
        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('fa/purchase_order', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );
        
        $this->data['token'] = $this->session->data['token'];
        
        $this->data['cancel'] = $this->url->link('common/home', 'token=' . $this->session->data['token'] . $url, 'SSL');
        
        //supplier
        $this->data['suppliers'] = array();
        
        $result_supplier = $this->model_fa_supplier->getSuppliers();
        if($result_supplier){            
            foreach($result_supplier as $result){                
                $this->data['suppliers'][] = array(
                    'supplier_id' => $result['supplier_id'],
                    'name' => $result['name']
                );
            }
        }
        
        $this->template = 'fa/purchase_order.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }
    
    //get item
    public function getPurchases(){
        
        $json = array();
        
        if (!empty($this->request->post['supplier_id'])) {
            $supplier_id = $this->request->post['supplier_id'];
        } else {
            $supplier_id = 0;   
        }
        
        $this->load->model('fa/purchase_order');

        $purchase_info = $this->model_fa_purchase_order->getPurchase($supplier_id);
        
         //get address
        $this->load->model('fa/supplier');
        
        $result_address = $this->model_fa_supplier->getAddress($supplier_id);
        
        foreach($result_address as $address){
            $address = $address['address'];
        }
        
        if($purchase_info){
            foreach ($purchase_info as $key => $purchase){
                $item_info = $this->model_fa_purchase_order->getItem($purchase['item_id']);
                
                $json['items'][] = array(
                    'item_id' => $item_info['item_id'],
                    'name' => $item_info['name'],
                    'address' => $address
                );
            }
        } 
        $this->response->setOutput(json_encode($json));
    }
}
?>
